package view;

import java.io.File;

import javax.servlet.Servlet;

import model.RequestsManager;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;

import servlets.AuthenticationServlet;
import servlets.DownloadServlet;
import servlets.HomeServlet;
import servlets.SharedFolderServlet;
import servlets.StopServerServlet;
import servlets.UploadServlet;
import servlets.UserDropboxServlet;
import servlets.WaitForMessageServlet;

/**
 * class MyServer - build the server parts - servlets and sockets
 */
public class MyServer {
	
	private static Server server;
	
	/**
	 * starts the server
	 * @param args
	 */
	public static void main(String[] args)
	{
		System.out.println("server side");
		
		
		
		String hibernateXmlPath = new String("C:\\clever dropbox server\\hibernate.cfg.xml");
		File dir = new File(hibernateXmlPath);
		
		if (!dir.exists())
		{		
			Thread thread = new Thread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					 GUIServer.GUIServerMain();
					
				}
			});
			 thread.start();
			 try {
				thread.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
			
			String sqlUser = GUIServer.getSqlUser();
			String sqlPassword = GUIServer.getSqlPass();
			
			if ((sqlUser==null) || (sqlPassword==null))
				return;
			
			System.out.println("create hibernate xml");
			File folder= new File("C:\\clever dropbox server");
			folder.mkdir();
			HibernateXML.createHibernateXml(hibernateXmlPath, sqlUser, sqlPassword);
		}
        
		
		File file1 = new File(".\\files");
		
		if (!file1.exists())
			file1.mkdir();
		
		File file = new File(".\\files\\a");
		file.mkdir();
		
		//sign the servlets
        server = new Server(12);
        Context root = new Context(server,"/",Context.SESSIONS);

        Servlet serverServlet =new HomeServlet();
        Servlet userDropboxServlet =new UserDropboxServlet();
        Servlet sharedServlet = new SharedFolderServlet();
        Servlet waitForMessageServlet = new WaitForMessageServlet();
        Servlet authenticationServlet = new AuthenticationServlet();
        
        
        root.addServlet(new ServletHolder(serverServlet), "/*");
        root.addServlet(new ServletHolder(serverServlet), "/index");
        root.addServlet(new ServletHolder(userDropboxServlet), "/UserDropboxServlet");
        root.addServlet(new ServletHolder(userDropboxServlet), "/UserDropboxServlet/folder/*");
        root.addServlet(new ServletHolder(sharedServlet), "/SharedFolderServlet/*");
        root.addServlet(new ServletHolder(waitForMessageServlet), "/WaitForMessageServlet/*");
        root.addServlet(new ServletHolder(authenticationServlet), "/AuthenticationServlet/*");
        
        Servlet uploadServlet = new UploadServlet(); 
        root.addServlet(new ServletHolder(uploadServlet), "/UploadServlet");
        root.addServlet(new ServletHolder(uploadServlet), "/UploadServlet/*");
        
        Servlet downloadServlet = new DownloadServlet();
        root.addServlet(new ServletHolder(downloadServlet), "/DownloadServlet");
        root.addServlet(new ServletHolder(downloadServlet), "/DownloadServlet/*");
        
        Servlet stopServerServlet = new StopServerServlet();
        root.addServlet(new ServletHolder(stopServerServlet), "/StopServerServlet");
        
        Thread socketServerTread = new Thread(GetRequestsFromUserSocket.getInstance());
        socketServerTread.start();    
        
        SendMessagesToUserSocket.getInstance().signAsObserver();
        Thread sendMessagesToUserSocketTread = new Thread(SendMessagesToUserSocket.getInstance());
        sendMessagesToUserSocketTread.start();

        try {
			server.start();
			server.join();
		} catch (Exception e) {
			// TODO Auto-generated catch block			
			e.getCause();
			e.getMessage();
			e.printStackTrace();
		}
        
		System.out.println("finish main");
		
	}
	
	/**
	 * stop the server
	 */
	public static void stop() {
		
		GetRequestsFromUserSocket.getInstance().stop();
		SendMessagesToUserSocket.getInstance().stop();
		RequestsManager.getInstance().stop();
		
		new Thread(){
			
			public void run() { 
				try {
					server.stop();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}.start();
		
		
	}
 
}
